package de.kimera.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import de.kimera.api.IEmployee;
import de.kimera.model.Employee;
import de.kimera.utils.Utils;


public class ChangeEmployeeDataDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField firstNameTextField;
	private JTextField lastNameTextField;
	private JTextField birthDateTextField;
	private JTextField addressTextField;
	private JTextField emailTextField;

	private IEmployee employee;
	
	private boolean employeeHasBeenChanged;
	/**
	 * Create the dialog.
	 */
	public ChangeEmployeeDataDialog() {
		setResizable(false);
		employeeHasBeenChanged = false;
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle(Utils.LABEL_TITLE_CHANGEEMPLOYEEDIALOG);
		setBounds(Utils.calculateBounds(Utils.DIALOG_CHANGEEMPLOYEE_WIDHT,Utils.DIALOG_CHANGEEMPLOYEE_HEIGHT));
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel firstNameLabel = new JLabel(Utils.LABEL_EMPLOYEE_FIRSTNAME);
			firstNameLabel.setBounds(5, 11, 90, 24);
			contentPanel.add(firstNameLabel);
		}
		{
			firstNameTextField = new JTextField();
			firstNameTextField.setBounds(91, 11, 260, 24);
			contentPanel.add(firstNameTextField);
			firstNameTextField.setColumns(10);
		}
		{
			JLabel lastNameLabel = new JLabel(Utils.LABEL_EMPLOYEE_LASTNAME);
			lastNameLabel.setBounds(5, 46, 90, 24);
			contentPanel.add(lastNameLabel);
		}
		{
			lastNameTextField = new JTextField();
			lastNameTextField.setBounds(91, 46, 260, 24);
			contentPanel.add(lastNameTextField);
			lastNameTextField.setColumns(10);
		}
		{
			JLabel birthDateLabel = new JLabel(Utils.LABEL_EMPLOYEE_BIRTHDATE);
			birthDateLabel.setBounds(5, 81, 90, 24);
			contentPanel.add(birthDateLabel);
		}
		{
			birthDateTextField = new JTextField();
			birthDateTextField.setBounds(91, 81, 260, 24);
			contentPanel.add(birthDateTextField);
			birthDateTextField.setColumns(10);
		}
		{
			JLabel addressLabel = new JLabel(Utils.LABEL_EMPLOYEE_ADDRESS);
			addressLabel.setBounds(5, 116, 90, 24);
			contentPanel.add(addressLabel);
		}
		{
			addressTextField = new JTextField();
			addressTextField.setBounds(91, 116, 260, 24);
			contentPanel.add(addressTextField);
			addressTextField.setColumns(10);
		}
		{
			JLabel emailLabel = new JLabel(Utils.LABEL_EMPLOYEE_EMAIL);
			emailLabel.setBounds(5, 151, 82, 24);
			contentPanel.add(emailLabel);
		}
		{
			emailTextField = new JTextField();
			emailTextField.setBounds(91, 151, 260, 24);
			contentPanel.add(emailTextField);
			emailTextField.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton(Utils.LABEL_BUTTON_SAVE);
				okButton.setName(Utils.LABEL_BUTTON_SAVE);
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						saveEmployee();
						dispose();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton(Utils.LABEL_BUTTON_CANCEL);
				cancelButton.setName(Utils.LABEL_BUTTON_CANCEL);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}

	public IEmployee getEmployee() {
		return employee;
	}

	private void saveEmployee(){
		employee.setFirstName(firstNameTextField.getText());
		employee.setLastName(lastNameTextField.getText());
		employee.setBirthDateString(birthDateTextField.getText());
		employee.setAddress(addressTextField.getText());
		employee.setEmailAddress(emailTextField.getText());
		
		employeeHasBeenChanged = true;
	}
	
	public void setEmployee(IEmployee employee) {
		this.employee = employee;
		firstNameTextField.setText(employee.getFirstName());
		lastNameTextField.setText(employee.getLastName());
		birthDateTextField.setText(employee.getBirthDateString());
		addressTextField.setText(employee.getAddress());
		emailTextField.setText(employee.getEmailAddress());
	}
	
	public boolean isEmployeeHasBeenChanged() {
		return employeeHasBeenChanged;
	}
}
